package com.yanyu.space.codegen.util;


import cn.hutool.db.ds.simple.SimpleDataSource;
import com.yanyu.space.codegen.bean.enums.CodegenRCode;
import com.yanyu.space.codegen.bean.vo.dssetting.ColumnInfoVo;
import com.yanyu.space.codegen.bean.vo.dssetting.TableInfoVo;
import com.yanyu.space.common.core.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态数据源
 *
 * @author yanyu
 * @date 2020/12/1
 */
@Slf4j
@Component
@AllArgsConstructor
public class DyncDataSourceUtil {


    public static TableInfoVo getTableInfo(String url, String dsName, String username, String password, String tableName) {
        Connection connection = null;
        Statement stmt = null;
        try {
            String dsUrl = StringUtils.join("jdbc:mysql://", url, "/", dsName, "?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf-8&useSSL=true");
            DataSource ds = new SimpleDataSource(dsUrl, username, password);
            connection = ds.getConnection();
            stmt = connection.createStatement();
            String sql = "select table_name as tableName, engine, table_comment as tableComment from information_schema.tables where table_schema = (select database()) and table_name = '" + tableName + "'";
            ResultSet rs = stmt.executeQuery(sql);
            TableInfoVo tableInfoVo = new TableInfoVo();
            while (rs.next()) {
                String engine = rs.getString("engine");
                String tableComment = rs.getString("tableComment");
                tableInfoVo.setTableName(tableName);
                tableInfoVo.setEngine(engine);
                tableInfoVo.setTableComment(tableComment);
            }
            return tableInfoVo;
        } catch (SQLException e) {
            throw new ServiceException(CodegenRCode.TESTDS_FAIL, e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                log.error("关闭数据库连接失败", e);
            }
        }
    }

    public static List<ColumnInfoVo> listColumn(String url, String dsName, String username, String password, String tableName) {
        Connection connection = null;
        Statement stmt = null;
        try {
            String dsUrl = StringUtils.join("jdbc:mysql://", url, "/", dsName, "?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf-8&useSSL=true");
            DataSource ds = new SimpleDataSource(dsUrl, username, password);
            connection = ds.getConnection();
            stmt = connection.createStatement();
            String sql = "select column_name as columnName, data_type as columnType, column_comment as columnComment, column_key as columnKey, extra from information_schema.columns" +
                    "  where table_name = '" + tableName +"' and table_schema = (select database()) order by ordinal_position";
            ResultSet rs = stmt.executeQuery(sql);
            List<ColumnInfoVo> columnInfoVos = new ArrayList<>();
            while (rs.next()) {
                String columnName = rs.getString("columnName");
                String columnType = rs.getString("columnType");
                String columnComment = rs.getString("columnComment");
                String columnKey = rs.getString("columnKey");
                ColumnInfoVo columnInfoVo = new ColumnInfoVo();
                columnInfoVo.setColumnName(columnName);
                columnInfoVo.setColumnType(columnType);
                columnInfoVo.setColumnComment(columnComment);
                columnInfoVo.setColumnKey(columnKey);
                columnInfoVos.add(columnInfoVo);
            }
            return columnInfoVos;
        } catch (SQLException e) {
            throw new ServiceException(CodegenRCode.TESTDS_FAIL, e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                log.error("关闭数据库连接失败", e);
            }
        }
    }
}
